package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.DetailVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {

        try {
            Thread.sleep(1100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //缺失信息:目的地ID   作者id   摘要    创建时间   是否是国外
        //大攻略下查询已经有了目的地封装,直接通过查询大攻略获取到目的地ID
        Strategy strategy = strategyService.get(strategyDetail.getStrategy().getId());

        if (strategy != null) {

            Destination dest = strategy.getDest();

            if (dest != null) {

                strategyDetail.setDest(dest);

                //是否是国外, 通过面包屑找到顶层
                List<Destination> toasts = destinationService.getToasts(dest.getId());

                Destination destination = toasts.get(0);

                if (destination.getId() != 1L) {
                    //国外
                    strategyDetail.setIsabroad(StrategyDetail.ABROAD_YES);
                } else {
                    //国内
                    strategyDetail.setIsabroad(StrategyDetail.ABROAD_NO);
                }

            }

        }

        //假数据 作者id


        //摘要
        String content = strategyDetail.getStrategyContent().getContent();

        if (content.length() > 200) {

            //判断一下长度,否侧会出现超界
            strategyDetail.setSummary(content.substring(0, 200));

        } else {
            strategyDetail.setSummary(content);
        }

        //创建时间
        if (strategyDetail.getId() == null) {

            strategyDetail.setCreateTime(new Date());

            strategyDetailMapper.insert(strategyDetail);

            //维护跟文章内容之间的关系
            StrategyContent strategyContent = strategyDetail.getStrategyContent();

            strategyContent.setId(strategyDetail.getId());

            strategyContentMapper.insert(strategyContent);

        } else {

            strategyDetailMapper.updateByPrimaryKey(strategyDetail);

            StrategyContent strategyContent = strategyDetail.getStrategyContent();

            strategyContent.setId(strategyDetail.getId());

            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }

        //先删去所有标签之间的关系
        strategyTagService.deleteRelationByDetailId(strategyDetail.getId());

        //维护跟标签之间的关系
        if (tags != null) {

            String[] tag = tags.split(",");

            for (String name : tag) {

                //添加之前先判断标签存不存在
                StrategyTag st = strategyTagService.getTagsByName(name);

                if (st == null) {

                    StrategyTag strategyTag = new StrategyTag();

                    strategyTag.setName(name);

                    strategyTagService.saveOrUpdate(strategyTag);

                    //维护关系
                    strategyTagService.insertRelation(strategyTag.getId(), strategyDetail.getId());

                } else {

                    strategyTagService.insertRelation(st.getId(), strategyDetail.getId());
                }

            }


        }

    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            strategyDetailMapper.deleteByPrimaryKey(id);

            //把内容删掉
            strategyContentMapper.deleteByPrimaryKey(id);

            //把标签关系删除
            strategyTagService.deleteRelationByDetailId(id);

        }
    }

    @Override
    public void changeState(Integer state, Long id) {
        if (state != null && id != null) {

            strategyDetailMapper.changeState(id, state);
        }
    }

    @Override
    public List<StrategyDetail> getDetailsByViewTop3(Long id) {

        if (id != null) {

            return strategyDetailMapper.getDetailsByViewTop3(id);
        }
        return null;
    }

    @Override
    public List<StrategyDetail> queryByCatalogId(Long catalogId) {

        if (catalogId != null) {

            return strategyDetailMapper.queryByCatalogId(catalogId);

        }
        return null;

    }

    @Override
    public void updateReplyNum(Long detailId, Integer i) {

        if (detailId != null && i != null) {

            strategyDetailMapper.updateReplyNum(detailId,i);
        }

    }

    @Override
    public int getReplyNumById(Long detailId) {

        if(detailId != null){

            return strategyDetailMapper. getReplyNumById(detailId);
        }


        return 0;
    }

    @Override
    public void setVO(DetailVo vo) {

        if(vo != null){

            strategyDetailMapper.setVO(vo);
        }


    }


}
